*0b assembled matlab solutions

	forval f = 1/34622 {
	cap {
	
		insheet using "/gpfs22/home/tany2/rho/x_rhos`f'.csv", comma clear
		drop if v1==0
		rename v1 fup12
		rename v2 year
		rename v3 fid2yr
		rename v4 svshare

		rename v5 omega1_a
		rename v6 omega1_a_ft
		rename v7 rho1_a
		rename v8 fval1_a
		rename v9 exit1_a

		rename v10 omega1_b
		rename v11 omega1_b_ft
		rename v12 rho1_b
		rename v13 fval1_b
		rename v14 exit1_b

		rename v15 omega1_c
		rename v16 omega1_c_ft
		rename v17 rho1_c
		rename v18 fval1_c
		rename v19 exit1_c

		rename v20 omega1_d
		rename v21 omega1_d_ft
		rename v22 rho1_d 
		rename v23 fval1_d
		rename v24 exit1_d
		
		destring rho*, replace force

	** Choose the RHO from the starting value that minimizes objective function **
	forval j = 1/1 {
		egen min`j' = rmin(fval`j'_*)
		g i`j' = ""
		foreach v in a b c d {
			replace i`j' = "`v'" if fval`j'_`v' == min`j'
			}
		g rho`j'   = .
		g omega`j' = .
		g exit`j'  = .
		foreach v in a b c d {
			replace rho`j'   = rho`j'_`v' if i`j' == "`v'"
			replace omega`j' = omega`j'_`v'_ft if i`j' == "`v'"
			replace exit`j'  = exit`j'_`v' if i`j' == "`v'"
			}
		}
		rename rho1 rho_x
		rename omega1 omega_x
		rename omega1_a omega_fjt_x
		rename min1 min_x
		rename exit1 exit_x

		order fup12 year fid2yr svshare *_x
		keep  fup12 year fid2yr svshare *_x
		save "/gpfs22/home/tany2/mat_rho/tmp_rhos_`f'.dta",replace
	}
		}
clear all

cd "/gpfs22/home/tany2/mat_rho"
!ls  tmp_rhos_*.dta  >\list.txt





	file open myfile using list.txt, read
	file read myfile line

	
	use `line',clear
	saveold "/gpfs22/home/tany2/rho2",replace

	
	
	file read myfile line
	while r(eof)==0 {
		append using `line'
		file read myfile line
	}
	file close myfile
	save "/gpfs22/home/tany2/rho2",replace

	use "/gpfs22/home/tany2/rho2",clear
	sort fup12 year
		local llist = "x"
		foreach j of local llist {
			g converge_`j' = exit_`j'==1|exit_`j'==3
			}
	drop *fjt*
	keep fid fup12 year *_x
	compress
	sort fup12 year
saveold "/gpfs22/home/tany2/rho2",replace
